package com.ansys.demo.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.*;


/**
 *
 * @author panghai
 * @Date: 2022/04/05/9:28
 * @Description: 读取word工具类
 */

public class DocUtil {

    public static List<String> chinese;
    public static List<String> zhuang;
    public static List<List<String>> options;
    public static List<String> imgList;

    private static String readDocxImage(File srcFile, String imageFile) {
        try {
            // 目录不存在时创建目录
            File pathFile = new File(imageFile);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            List<String> list = new ArrayList<>();
            // 用XWPFWordExtractor来获取文字
            FileInputStream fis = new FileInputStream(srcFile);
            XWPFDocument document = new XWPFDocument(fis);
            XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(document);
            String text = xwpfWordExtractor.getText();
            processStr(text);
            // 用XWPFDocument的getAllPictures来获取所有的图片
            List<XWPFPictureData> picList = document.getAllPictures();
            document.getBodyElementsIterator();
            for (XWPFPictureData pic : picList) {
                byte[] bytev = pic.getData();
                String name = pic.getFileName();
                FileOutputStream fos = new FileOutputStream(imageFile + "/" + name);
                list.add(name);
                fos.write(bytev);
            }
            imgList=list;
            fis.close();
            return text;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void processStr(String str) {
        String HAN = "汉义：";
        String ZHUANG = "壮文：";
        String OPTION = "设计选项：";
        String A="A.";
        String B="B.";
        String C="C.";
        String D="D.";
        int NOT_MATCH = -1;
        List<String> c = new ArrayList<>();
        List<String> z = new ArrayList<>();
        List<List<String>> o = new ArrayList<>();
        String[] s = str.split("\n");
        for (int j = 0; j < s.length; j++) {
            String sd = s[j];
            if(!"".equals(sd)){
                if (sd.indexOf(HAN)!=NOT_MATCH){
                    String[] st = sd.split(HAN);
                    c.add(st[1]);
                }else if (sd.indexOf(ZHUANG)!=NOT_MATCH){
                    String[] st = sd.split(ZHUANG);
                    z.add(st[1]);
                }else if (sd.indexOf(OPTION)!=NOT_MATCH){
                    String[] st = sd.split(OPTION);
                    String optionString = st[1].replaceAll("\\s+","");
                    int indexA = optionString.indexOf(A);
                    int indexB = optionString.indexOf(B);
                    int indexC = optionString.indexOf(C);
                    int indexD = optionString.indexOf(D);
                    List<String> stList = new ArrayList<>();
                    stList.add(optionString.substring(indexA+2,indexB));
                    stList.add(optionString.substring(indexB+2,indexC));
                    stList.add(optionString.substring(indexC+2,indexD));
                    stList.add(optionString.substring(indexD+2));
                    o.add(stList);
                }
            }
        }
        options = o;
        chinese = c;
        zhuang = z;
    }

    public static void readWordImage(String fileName, File file, String savePath) {
        if (null == savePath) {
            savePath = "";
        }
        if (fileName.endsWith(".docx")) {
            readDocxImage(file, savePath);
        } else {
            return;
        }
    }

}
